package com.business_clean.ui.adapter.camera;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.business_clean.R;
import com.business_clean.app.ext.CommonUtils;
import com.business_clean.data.mode.camera.LeaderClockListEntity;
import com.business_clean.data.mode.camera.LeaderClockUserListEntity;
import com.business_clean.ui.activity.attendance.MyAttendanceActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lxj.xpopup.interfaces.OnSelectListener;

import java.util.HashMap;

public class LeaderAdapter extends BaseQuickAdapter<LeaderClockListEntity, BaseViewHolder> {
    public LeaderAdapter() {
        super(R.layout.item_leader);
        addChildClickViewIds(R.id.tv_item_leader_add);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, LeaderClockListEntity item) {
        holder.setText(R.id.tv_item_leader_name, item.getClass_name());
        RecyclerView recyclerView = holder.getView(R.id.tv_item_leader_recycler);
        LeaderItemAdapter leaderItemAdapter = new LeaderItemAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
        recyclerView.setAdapter(leaderItemAdapter);
        leaderItemAdapter.setList(item.getUser_list());

        leaderItemAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                if (view.getId() == R.id.tv_item_leader_item_jiaban) {
                    leaderItemAdapter.removeAt(position);//删除掉加班人员
                }
            }
        });

        leaderItemAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (!leaderItemAdapter.getData().get(position).isAdd()) {//只有不是新增的才可以修改数据
                    showDialog(leaderItemAdapter.getData().get(position), leaderItemAdapter);
                }
            }
        });
    }

    /**
     * 更新 item 的数据
     */
    private void showDialog(LeaderClockUserListEntity data, LeaderItemAdapter leaderItemAdapter) {
        String[] strings = {"改为休假", "改为正常出勤", "改为旷工", "改为休息"};
        CommonUtils.showBottomListNoCheckedWith(getContext(), 0, "请选择出勤情况", strings, 0, new OnSelectListener() {
            @Override
            public void onSelect(int position, String text) {
                String status = "";//1休假 2正常出勤  4旷工 5休息 6加班
                String statusName = "";
                switch (position) {
                    case 0:
                        status = "1";
                        statusName = "休假";
                        break;
                    case 1:
                        status = "2";
                        statusName = "出勤";
                        break;
                    case 2:
                        status = "4";
                        statusName = "旷工";
                        break;
                    case 3:
                        status = "5";
                        statusName = "休息";
                        break;
                }
                data.setStatus(status);
                data.setStatus_name(statusName);
                if (leaderItemAdapter != null) {
                    leaderItemAdapter.notifyDataSetChanged();
                }
            }
        });
    }
}
